package org.axon.jmsexample.service;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.axon.jmsexample.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

@Service
public class OrderSender {

	@Autowired
	private JmsTemplate jmsTemplate;

	public void sendOrder(final Order order) {
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				MapMessage mapMessage = session.createMapMessage();
				mapMessage.setObject("socialType", order.getSocialType());
				mapMessage.setObject("method", order.getMethod());
				return mapMessage;
			}
		});
		System.out.println("Order sent - social type : " + order.getSocialType());
	}
}